<?php
namespace Mentees\Controller;

class MymentorController extends MainController {

    public function index(){
 
        $cate = I('get.cate');
    	$map['show']=1;
    	$train = M('Train')->field('id,name')->where($map)->order('sort asc,id asc')->select();
    	$this->assign('train',$train);
    	$this->assign('cate',$cate);

    	unset($map);
    	if ($cate) {
    		$map['trainItem'] = array('like','%-'.$cate.'-%');
    	}
		$obj = M('Mentors');
		$count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 9);
        $page->setConfig('prev',L('_prev_page'));
        $page->setConfig('next',L('_next_page'));
        $page->setConfig('first',L('_first_page'));
        $page->setConfig('end',L('_end_page'));
        $page->setConfig('theme','%first% %upPage% %linkPage% %downPage% %end%');
		$show = $page->show();
		$list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();	
		foreach ($list as $key => $value) {
            $value['trainItem'] = trainStrtoArr($value['trainItem']);
            $where['id'] = array('in',$value['trainItem']);
            $list[$key]['trainItem'] = M('Train')->field('name')->where($where)->select();
		}	
		$this->assign('list', $list);
		$this->assign('page', $show);
		
    	$this->display();
    }

    public function detail(){
        $id = I('get.id');
        if(empty($id)){
            $this->error("Illegal access");
        }else{
            $map['userid']=$id;
            $list = M('Mentors')->where($map)->find();
            if (!$list) {
                $this->error('The information does not exist');
            }

            $list['trainItem'] = trainStrtoArr($list['trainItem']);
            $where['id'] = array('in',$list['trainItem']);
            $list['trainItem'] = M('Train')->field('name')->where($where)->select();
            $this->assign('list',$list);
            $this->display();
        }
    }
    //拜师请求发送
    public function apply(){
        $toID = I('post.toID');
        $mentors = M('Mentors')->where('userid='.$toID)->find();
        if (!$mentors) {
            echo $this->echo_json_str(L('_not_exist'));die;
        }
        $list = M('Mentees')->where('userid='.$this->userID)->find();
        $data['fromID']=$this->userID;
        $data['fromName'] = $list['nickname'];
        $data['fromFace'] = $list['face'];
        $data['toID']=$toID;
        $data['toName']=$mentors['nickname'];
        $data['toFace']=$mentors['face'];
        $data['trainItem']=I('post.trainItem');
        $data['skills']=I('post.skills');
        $data['target']=I('post.target');
        $data['train']=I('post.train');
        $data['work']=I('post.work');
        $data['createTime'] = time(); 
        $data['updateTime'] = time(); 
        $list = M('Apprentice')->add($data);
        if ($list) {
            echo $this->echo_json_str("SUCCESS",L('_action_success'));die;
        }else{
            echo $this->echo_json_str(L('_action_fail'));die;
        }
    }
    public function comment(){
        $this->display();
    }
}
